clear
set more off
cap log close
program drop _all
set matsize 10000




********************************************
* If you don't want to run the code from the master do-file (JFEguide_masterscript), update the root directory filepath global here. If you run the code from the master do-file, it will control the root directory filepath and ignore the filepath below.
* If you don't want to run the code from the master do-file (JFEguide_masterscript), update the root directory filepath global here

if "$master" == ""	{
****************************
*** UPDATE FILEPATH HERE ***
****************************
global root_dir /Users/andersee/Dropbox/cluster_jackknife/restat/cjive_replication_package
*****************************

*** subfolders
global raw_data  $root_dir/raw_data
global intermediate_data $root_dir/intermediate_data
global clean_data $root_dir/clean_data
global tabfig $root_dir/tabfig
global logfiles $root_dir/logfiles
}
********************************************



log using $logfiles/analyze_Miami_data.log, replace

local start_time_analyze = clock(c(current_date) + " " + c(current_time), "DMYhms")
display "Start time analyze: " %tcCCYY-NN-DD_HH:MM:SS `start_time_analyze'


set seed 242

* globals
global esttab_options b(2) se(2)
global x_defcasechar = "male black age prior_offender_ever n_counts highest_charge_felony any_drug any_violent any_property"
global x_defcasechar100 = "male100 black100 age100 prior_offender_ever100 n_counts100 highest_charge_felony100 any_drug100 any_violent100 any_property100"
global x_timeplace fe_*




use $clean_data/clean_Miami_data, clear
keep if n_cases >= 200

* Fix shift var so it won't have any missing numbers (after the sample restriction on cases/judge)
egen temp = group(shift)
replace shift = temp
drop temp




***************************
*** TABLE: Second stage ***
***************************
estimates clear

*** Column 1: OLS
reg any_guilty bail_met $x_timeplace, r cluster(shift)


est sto col1


*** Column 2: 2SLS with judge dummies
ivreg2 any_guilty (bail_met = jfe_*) $x_timeplace, r cluster(shift)


est sto col2



* Get continuous instrument for estimating first stage
xtreg bail_met $x_timeplace, fe i(judgeid)
predict Zjdummy, u



*** Column 3: IJIVE (individual jackknife)
// NOTE: We'll use these residualized vars again for the cluster jackknife
summ judgeid
local jmax = `r(max)'
foreach var of varlist any_guilty bail_met jfe_judgeid_2-jfe_judgeid_`jmax' 	{
	
	qui reg `var' $x_timeplace
	predict `var'_r, resid
	
}

* Construct continuous instrument
	* OLS first stage
reg bail_met_r jfe_judgeid_*_r

	* save fitted valued (would use this if doing vanilla 2SLS)
predict dhat0

	* leverage = diagonal elements of projection matrix (instead of running N regressions) - gives each obs contribution to fitted values
predict h,leverage
gen Zijive_i = (dhat0 - h*bail_met_r)/(1-h)

// NOTE: Not using jive because no cluster option
ivregress 2sls any_guilty_r (bail_met_r = Zijive_i) $x_timeplace, vce(cluster shift)


est sto col3

drop dhat0 h




*** Column 4: CJIVE

cjive any_guilty (bail_met = jfe_*) $x_timeplace, cluster(shift) gen(Zijive_c)

est sto col4

/* Without cjive package:
summ shift
local n_shifts = `r(max)'

gen Zijive_c=.
forval i = 1/`n_shifts'	{
	
	qui reg bail_met_r jfe_judgeid_*_r if shift!=`i'
	predict temp, xb
	replace Zijive_c = temp if shift==`i'
	drop temp
	
}

ivregress 2sls any_guilty_r (bail_met_r = Zijive_c), vce(cluster shift)

estadd local jk "Cluster"
estadd local Xs "No"

est sto col4
*/



*** Make the table of 2nd stage estimates
esttab col1 col2 col3 col4 using $tabfig/secondstage.tex, replace ///
	rename(bail_met_r bail_met bail_met_rx bail_met) ///
	keep(bail_met) ///
	varlabels(bail_met "Released") ///
	mtitles("OLS" "Judge dummies" "IJIVE" "CJIVE") ///
	collabels(,none) ///
	noobs ///
	cells(b(fmt(3)) se(fmt(3) par)) ///
	nonum booktabs


	

	
	
***************************
*** TABLE: Balance test ***
***************************
estimates clear
reg Zijive_c $x_timeplace $x_defcasechar100,  r cluster(shift)
test $x_defcasechar
 
estadd scalar jointF = r(F)
estadd scalar jointp = r(p)

est sto baltest

esttab baltest using $tabfig/balancetest.tex, replace ///
	rename(male100 male black100 black age100 age prior_offender_ever100 prior_offender_ever n_counts100 n_counts highest_charge_felony100 highest_charge_felony any_drug100 any_drug any_violent100 any_violent any_property100 any_property) ///
	keep(male black age prior_offender_ever n_counts highest_charge_felony any_drug any_violent any_property) ///
	varlabels(male "Male" black "Black" age "Age" prior_offender_ever "Prior offender" n_counts "Number of counts" highest_charge_felony "Felony charge" any_drug "Drug charge" any_violent "Violent charge" any_property "Property charge") ///
	collabels(,none) ///
	nomtitles ///
	noobs ///
	cells(b(fmt(3)) se(fmt(3) par)) ///
	stats(jointF jointp, fmt(%18.3g %18.3g) labels("Joint F stat" "p-value")) ///
	nonum booktabs

	
* Calculate runtime of do-file
local end_time_analyze = clock(c(current_date) + " " + c(current_time), "DMYhms")
display "End time analyze: " %tcCCYY-NN-DD_HH:MM:SS `end_time_analyze'

	* Calculate time difference in milliseconds
	local duration_ms = `end_time_analyze' - `start_time_analyze'

	* Convert milliseconds to hours (1 hour = 3,600,000 ms)
	local duration_hours = `duration_ms' / 3600000

	* Display the result
	display "Duration analyze: " `duration_hours' " hours"
	
	
	
log close

